
import re
from io import BytesIO

from PIL import ImageFont, Image, ImageDraw
from fontTools.ttLib import TTFont
import ddddocr
import lxml.etree as le
url = 'index3.html'
# response = requests.get(url)
with open(url, 'r', encoding='utf-8') as f:
    response = f.read()

html_xs = le.HTML(response)
page_text = html_xs.xpath("//div//text()")
page_text = ''.join(page_text)
with open('style.css', 'r', encoding='utf-8') as f:
    css_data = f.read()

font = TTFont("ae0f8407.woff")
# print(font.getGlyphNames())  # unicode
# # 对unicode 进行处理 unie412 -> \uxxx


unicode_names = [name.replace("uni", r"\u") for name in font.getGlyphNames()]
dict_map = {}
unicode_names.remove("x")
unicode_names.remove("glyph00000")
print(unicode_names)
f = open("ae0f8407_copy.woff", mode="rb")
img_font = ImageFont.truetype(f, size=600)
for code in unicode_names:
    glyph_unicode = code.encode().decode("unicode-escape")
    # 加载字节文件，给图片用
    img = Image.new("1", (600, 1000), 255)  # 在内存
    draw = ImageDraw.Draw(img)
    # 向图片上书写文字
    draw.text((0, 0), glyph_unicode, font=img_font)
    # 图片 用bytesIO进行过渡
    bio = BytesIO()
    img.save(bio, "png")
    with open('xx.png','wb') as f:
        f.write(bio.getvalue())
    # 识别文字
    ocr = ddddocr.DdddOcr(show_ad=False, ocr=True)
    result = ocr.classification(bio.getvalue())  # 识别文字
    print(result)
    dict_map[glyph_unicode] = result
for k, v in dict_map.items():
    page_text = page_text.replace(k, v)
print(re.sub(r'\s',"",page_text))